package com.duowan.realtime.uniq.algorithm.hyperloglog.util;

import java.util.ArrayList;
import java.util.List;

import com.duowan.realtime.uniq.algorithm.bloomfilter.BloomFilter;
import com.duowan.realtime.uniq.algorithm.hyperloglog.HyperLogLog;

public class PositiveMain {
	public static void main(String[] args) throws Exception {
		testBF();
		//testHll();
	}

	private static void testBF() throws Exception {
		int[] elements = {10000,100000,1000000,10000000,30000000,50000000,1000000000,200000000,300000000,400000000};
		for (int i = 0; i < elements.length; i++) {
			BloomFilter bf = new BloomFilter(Integer.MAX_VALUE, 5);
			int count = 0;
			for (int j=0;j<elements[i];j++) {
				String v = ""+j;
				if (!bf.contains(v.getBytes("UTF-8"))) {
					bf.add(v.getBytes("UTF-8"));
					count ++ ;
				}
			}
			System.out.println("bloomfilter:raw count=" + elements[i] + ",result count="+count + ",positive="+Math.abs(count - elements[i])/(double)elements[i]);
			bf = null;
		}
		
	}

	private static List<String> genDatas(int count) {
		List<String> dataStrings = new ArrayList<String>();
		for(int i = 0; i < count; i++) {
			dataStrings.add(""+i);
		}
		return dataStrings;
	}
	
	private static void testHll() {
		int[] elements = {10000,100000,1000000,10000000,30000000,50000000,100000000,1000000000};
		for (int i = 0; i < elements.length; i++) {
			HyperLogLog hyperLogLog = new HyperLogLog(0.001);
			List<String> datas = genDatas(elements[i]);
			for (String v : datas) {
				hyperLogLog.offer(v);
			}
			long result = hyperLogLog.cardinality();
			System.out.println("hyperloglog:raw count="+elements[i]+",result count="+result+",positive="+Math.abs(result-elements[i])/(double)elements[i]);
		}
		
	}
}
